#include <stdio.h>
#include <string.h>
#include "ifetcher.h"





void CFetcherBase::set_fetch_address(bool mode,unsigned int _ip_)
{

	//
	// save pointer
	//
	thumb_mode = mode;

 

	if (thumb_mode)
	{
		pc = _ip_ & 0xffff;
	}
	else {
		pc = _ip_;
	}
}

//
//  return
//        0: illegal instruction
//        1: success
//        2: end
//
int CFetcherBase::fetch(PINSN_BASIC_INFO p_insn )
{


	unsigned int len;
;
	


	//
	// no matter of the result, fill fetch ip so that caller knows about it.
	//
	p_insn->pc=pc;

	//
	// end of insn stream or mmu fetch fault
	//
	if (!bytes ||!buffer) 
		return FETCH_EOF; // no data

	

	unsigned int code_len=thumb_mode?2:4;

	
	len=code_len;

	if (bytes<len) 
		return FETCH_EOF;

	if(thumb_mode){
		p_insn->thumb_insn.code=*(short *)buffer;
	} else {
		p_insn->arm_insn.code=*(int *)buffer;
	}

	p_insn->thumb=thumb_mode;

	pc +=len;
	buffer+=len;
	bytes-=len;

	return FETCH_SUCCESS;

}
